# -*- coding: utf-8 -*-
import json
import re
from math import radians, cos, sin, asin, sqrt

import exifread
from numpy import genfromtxt, transpose


def get_address_distance(address_img, address_txt):
    lon1, lat1, lon2, lat2 = map(radians, [address_img[0], address_img[1], address_txt[0], address_txt[1]])
    dlon = lon2 - lon1
    dlat = lat2 - lat1
    a = sin(dlat / 2) ** 2 + cos(lat1) * cos(lat2) * sin(dlon / 2) ** 2
    c = 2 * asin(sqrt(a))
    r = 6371
    dis = round((c * r * 1000) / 1000, 5)
    return dis


def read_gps_txt():
    data = genfromtxt('GPS.txt', delimiter=',')
    return transpose([data[:, 1], data[:, 0]])


def convert_gps(coord_arr):
    arr = str(coord_arr).replace('[', '').replace(']', '').split(', ')
    d = float(arr[0])
    m = float(arr[1])
    s = float(arr[2].split('/')[0]) / float(arr[2].split('/')[1])
    return float(d) + (float(m) / 60) + (float(s) / 3600)


def run():
    img_path = r"fontimg/datav/img1/DJI_0787.JPG"
    f = open(img_path, 'rb')
    contents = exifread.process_file(f)
    lon = convert_gps(contents['GPS GPSLongitude'].printable)
    lat = convert_gps(contents['GPS GPSLatitude'].printable)
    f.close()

    gpses = read_gps_txt()
    for gps in gpses:
        print(get_address_distance(gps, [lon, lat]))


def save_to_file(file_name, contents):
    fh = open(file_name, 'w')
    fh.write(contents)
    fh.close()


def assembly_number_json():
    with open('test.json', 'r', encoding='utf8') as f:
        json_data = json.load(f)
        print(json_data)
        for data in json_data:
            value = data["value"]
            content = data["content"]
            data["content"] = "第"
            print(content)
        dicts = json_data
    dicts = str(dicts).replace("'", "\"")
    save_to_file("number.json", str(dicts))


def a():
    str1 = '第12张'
    re1 = r'第(.*?)张'
    reResult = re.findall(re1, str1)
    print(int(str(reResult[0])))


if "__main__" == __name__:
    a()
    # assembly_json()
    # run()
